/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: 7.26client.c
> Author:wyb
> Mail: 
> Created Time: 2024年07月26日 星期五 11时45分04秒
***************************************************/

#include "7.26client.h"
#include "7.23client.h"
#include "7.26client.h"
#include "sys_log.h"
//注册员工账号  
void Registeranaccount()
{
	char type[30] = {"Employeeregistration"};
		
	struct staff da;
	memset(&da,0,sizeof(da));
	
	
	printf("输入员工编号，姓名，性别，年龄，账号，密码：");
	scanf("%d%s%s%d%d%d",&da.uid,da.name,da.gender,&da.age,&da.account,&da.cipher);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"uid",cJSON_CreateNumber(da.uid));
	cJSON_AddItemToObject(json,"name",cJSON_CreateString(da.name));
	cJSON_AddItemToObject(json,"gender",cJSON_CreateString(da.gender));
	cJSON_AddItemToObject(json,"age",cJSON_CreateNumber(da.age));
	cJSON_AddItemToObject(json,"account",cJSON_CreateNumber(da.account));
	cJSON_AddItemToObject(json,"cipher",cJSON_CreateNumber(da.cipher));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
	
	//调申请连接函数
	int sock = 0;
	int ret = 0;
	int ret1 = 0;
	int value = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		//perror("send");
		LOG_E("send");
		exit(-1);
	
	}
	
	//接受
	ret1 = recv(sock,&value,4,0);
	if(ret1 == -1)
	{
		LOG_E("recv");
		exit(-1);
	}
	
	if(value == 1)
	{
		printf("注册失败\n");
	}
	else if(value == 0)
	{
		printf("注册成功\n");
	}
	//释放空间和对象
	free(p);
	cJSON_Delete(json);
}
//注册管理员账号  
void Registeredadministrator()
{
	char type[30] = {"Administratorregistration"};
	struct Administrator da;
	memset(&da,0,sizeof(da));
	
	printf("输入员工编号，姓名，性别，年龄，账号，密码：");
	scanf("%d%s%s%d%d%d",&da.id,da.name,da.gender,&da.age,&da.account,&da.cipher);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(da.id));
	cJSON_AddItemToObject(json,"name",cJSON_CreateString(da.name));
	cJSON_AddItemToObject(json,"gender",cJSON_CreateString(da.gender));
	cJSON_AddItemToObject(json,"age",cJSON_CreateNumber(da.age));
	cJSON_AddItemToObject(json,"account",cJSON_CreateNumber(da.account));
	cJSON_AddItemToObject(json,"cipher",cJSON_CreateNumber(da.cipher));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
	
	//调申请连接函数
	int sock = 0;
	int ret = 0;
	int ret1 = 0;
	int value = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	
	ret1 = recv(sock,&value,4,0);
	if(ret1 == -1)
	{
		LOG_E("recv");
		exit(-1);
	}
	
	if(value == 1)
	{
		printf("注册成功\n");
	}
	else if(value == 0)
	{
		//printf("注册失败\n");
	}
	//释放空间和对象
	free(p);
	cJSON_Delete(json);
}
//登陆员工账号
void Employeeaccount()
{
	char type[30] = {"Employeelogin"};
	struct log da;
	memset(&da,0,sizeof(da));
	
	printf("输入员工账号，密码：");
	scanf("%d%d",&da.account,&da.cipher);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"account",cJSON_CreateNumber(da.account));
	cJSON_AddItemToObject(json,"cipher",cJSON_CreateNumber(da.cipher));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
	
	//调申请连接函数
	int sock = 0;
	int ret = 0;
	int ret1 = 0;
	int value = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	//接受
	ret1 = recv(sock,&value,4,0);
	if(ret1 == -1)
	{
		LOG_E("recv");
		exit(-1);
	}
	
	if(value == 1)
	{
		printf("登陆成功\n");
		caidan1();
	}
	else if(value == 0)
	{
		printf("登陆失败\n");
	}
	
	//释放空间和对象
	free(p);
	cJSON_Delete(json);
}
//登陆管理员账号
void Administratoraccount()
{
	char type[30] = {"Administratorlogin"};
	struct log1 da;
	memset(&da,0,sizeof(da));
	
	printf("输入员工账号，密码：");
	scanf("%d%d",&da.account,&da.cipher);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"account",cJSON_CreateNumber(da.account));
	cJSON_AddItemToObject(json,"cipher",cJSON_CreateNumber(da.cipher));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
	
	//调申请连接函数
	int sock = 0;
	int ret = 0;
	int ret1 = 0;
	int value = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	
	//接受
	ret1 = recv(sock,&value,4,0);
	if(ret1 == -1)
	{
		LOG_E("recv");
		exit(-1);
	}
	
	if(value == 1)
	{
		printf("登陆成功\n");
		caidan2();
	}
	else if(value == 0)
	{
		printf("登陆失败\n");
	}
	
	//释放空间和对象
	free(p);
	cJSON_Delete(json);
}


